package com.ruoyi.web.admin.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDeviceLocation;
import com.ruoyi.system.service.ISysDeviceLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 10100
 */
@Api(tags = "设备位置信息管理")
@RestController
@RequestMapping("/system/deviceLocation")
public class SysDeviceLocationController extends BaseController {
    private final ISysDeviceLocationService sysDeviceLocationService;

    public SysDeviceLocationController(ISysDeviceLocationService sysDeviceLocationService) {
        this.sysDeviceLocationService = sysDeviceLocationService;
    }

    @ApiOperation("查询设备位置信息列表")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDeviceLocation sysDeviceLocation) {
        startPage();
        List<SysDeviceLocation> list = sysDeviceLocationService.selectSysDeviceLocationList(sysDeviceLocation);
        return getDataTable(list);
    }

    @ApiOperation("获取设备位置信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("位置ID") @PathVariable("id") Long id) {
        return success(sysDeviceLocationService.selectSysDeviceLocationById(id));
    }

    @ApiOperation("新增设备位置信息")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:add')")
    @Log(title = "设备位置信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeviceLocation sysDeviceLocation) {
        return toAjax(sysDeviceLocationService.insertSysDeviceLocation(sysDeviceLocation));
    }

    @ApiOperation("修改设备位置信息")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:edit')")
    @Log(title = "设备位置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeviceLocation sysDeviceLocation) {
        return toAjax(sysDeviceLocationService.updateSysDeviceLocation(sysDeviceLocation));
    }

    @ApiOperation("删除设备位置信息")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:remove')")
    @Log(title = "设备位置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("位置ID数组") @PathVariable Long[] ids) {
        return toAjax(sysDeviceLocationService.deleteSysDeviceLocationByIds(ids));
    }

    @ApiOperation("查询设备最新位置信息")
    @GetMapping("/latest/device/{deviceId}")
    public AjaxResult getLatestByDeviceId(@ApiParam("设备ID") @PathVariable("deviceId") String deviceId) {
        return success(sysDeviceLocationService.selectLatestLocationByDeviceId(deviceId));
    }

    @ApiOperation("查询用户和设备的最新位置信息")
    @GetMapping("/latest/user/{userId}/device/{deviceId}")
    public AjaxResult getLatestByUserIdAndDeviceId(@ApiParam("用户ID") @PathVariable("userId") Long userId,
                                                   @ApiParam("设备ID") @PathVariable("deviceId") Long deviceId) {
        return success(sysDeviceLocationService.selectLatestLocationByUserIdAndDeviceId(userId, deviceId));
    }

    @ApiOperation("查询设备历史轨迹")
    @GetMapping("/track/{deviceId}")
    public AjaxResult getDeviceTrack(@ApiParam("设备ID") @PathVariable("deviceId") String deviceId,
                                     @ApiParam("开始时间") @RequestParam Date startTime,
                                     @ApiParam("结束时间") @RequestParam Date endTime) {
        List<SysDeviceLocation> list = sysDeviceLocationService.selectDeviceTrackByTime(deviceId, startTime, endTime);
        return success(list);
    }

    @ApiOperation("清理历史位置数据")
    @PreAuthorize("@ss.hasPermi('system:deviceLocation:clean')")
    @DeleteMapping("/clean/{days}")
    public AjaxResult cleanHistory(@ApiParam("保留天数") @PathVariable int days) {
        return toAjax(sysDeviceLocationService.cleanHistoryLocationData(days));
    }
} 